package com.ymk.ctrl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.cmd.SchedulerCmd;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.pay.PayPaymentBO;
import com.ymk.core.code.cmd.PayPaymentCmdCode;
import com.ymk.core.constants.SysConstants;
import com.ymk.mq.MessageSender;
import com.ymk.utlis.WxPayUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;

@RestController
public class WxPayHandlerCtrl {

    private final static Logger logger = LoggerFactory.getLogger(WxPayHandlerCtrl.class);


    @RequestMapping(path = "/wxPay/payNotify", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> wxPayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String readData = WxPayUtil.readData(request);
        logger.info(readData);
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild();
        try {
            JSONObject param = JSON.parseObject(readData);
            //支付成功的处理
            if (ObjectUtils.isNotEmpty(param) && "SUCCESS".equals(param.get("returnCode").toString())){

                logger.info(JSON.toJSONString(param));
                String transaction_id = param.get("transactionId").toString();
                String out_trade_no = param.get("outTradeNo").toString();
                String total_amount = param.get("totalFee").toString();

                PayPaymentBO payPaymentBO = new PayPaymentBO();
                payPaymentBO.setPayNo(out_trade_no);
                payPaymentBO.setTradeNo(transaction_id);
                payPaymentBO.setAmount(new BigDecimal(total_amount));
                payPaymentBO.setSyncData(readData);

                CmdSign build = CmdSign.Builder.newBuild(null)
                        .setSource(payPaymentBO)
                        .setCmdCode(PayPaymentCmdCode.PAY_PAYMENT_NOTIFY)
                        .setToken(SysConstants.DEFAULT_ADMIN_TOKEN)
                        .build();
                this.scheduler.schedule(request, build);
            }
            //成功应答
            paramsBuilder.clearParam()
                    .addParam("errcode","0");
            return paramsBuilder.build();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            //应答失败
            return paramsBuilder.build();
        }
    }


    @Autowired
    private MessageSender messageSender;
    @Autowired
    private SchedulerCmd scheduler;
}
